#include "TUploadDialog.h"

#include "TContext.h"
#include "TMessageBox.h"

#include <QGridLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QLineEdit>
#include <QComboBox>
#include <QProgressBar>
#include <QHBoxLayout>
#include <QLabel>

#include <QDebug>

const int TUploadDialog::ProgressWeight = 10000;

TUploadDialog::TUploadDialog(QWidget *parent)
    :TSkinDialog(parent, Qt::MSWindowsFixedSizeDialogHint)
{
    this->setMinimumSize(300, 200);
    this->setWindowTitle(tr("file upload"));
    this->resize(350, 200);

    setupSelectFrame();
    setupUploadingFrame();
    setupFinishedFrame();

    QVBoxLayout* layout = new QVBoxLayout(getClient());
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    layout->addWidget(_selectFrame);
    layout->addWidget(_uploadingFrame);
    layout->addWidget(_finishedFrame);

    this->_uploadingFrame->hide();
    this->_finishedFrame->hide();
}

void TUploadDialog::setupSelectFrame()
{
    _selectFrame = new QFrame();
    _selectFrame->resize(getClient()->size());
    QGridLayout* layout = new QGridLayout(_selectFrame);
    layout->setContentsMargins(50, 10, 15, 10);
    layout->setVerticalSpacing(6);
    layout->setHorizontalSpacing(0);
    layout->setColumnStretch(3, 65535);

    //row 1
    QLabel* lbtFile = new QLabel(_selectFrame);
    lbtFile->setText(tr("file"));
    lbtFile->setAlignment(Qt::AlignRight);
    lbtFile->setMargin(5);
    layout->addWidget(lbtFile, 1, 1, 1, 1);

    _ledFile = new QLineEdit(_selectFrame);
    _ledFile->setFixedHeight(25);
    layout->addWidget(_ledFile, 1, 2, 1, 4);

    _pbtSelect = new QPushButton(_selectFrame);
    _pbtSelect->setText(tr("select"));
    _pbtSelect->setFixedHeight(25);
    _pbtSelect->setFixedWidth(35);
    layout->addWidget(_pbtSelect, 1, 6, 1, 1);

    // row 2
    QLabel* lbtTitle = new QLabel(_selectFrame);
    lbtTitle->setText(tr("title"));
    lbtTitle->setFixedHeight(25);
    lbtTitle->setMargin(5);
    layout->addWidget(lbtTitle, 2, 1, 1, 1);

    _ledTitle = new QLineEdit(_selectFrame);
    _ledTitle->setFixedHeight(25);
    layout->addWidget(_ledTitle, 2, 2, 1, 4);

    // row 3
    QLabel* lbtShare = new QLabel(_selectFrame);
    lbtShare->setText(tr("share"));
    lbtShare->setFixedHeight(25);
    lbtShare->setMargin(5);
    layout->addWidget(lbtShare, 3, 1, 1, 1);

    _cmbShare = new QComboBox(_selectFrame);
    _cmbShare->addItem(tr("not share"));
    _cmbShare->addItem(tr("share"));
    _cmbShare->setFixedHeight(25);
    layout->addWidget(_cmbShare, 3, 2, 1, 1);

    //row 4
    _pbtUpload = new QPushButton(_selectFrame);
    _pbtUpload->setText(tr("upload"));
    _pbtUpload->setFixedHeight(25);
    layout->addWidget(_pbtUpload, 5, 5, 1, 2);

    //connection
    connect(_pbtSelect, SIGNAL(clicked()), this, SLOT(onSelectFileClicked()));
    connect(_pbtUpload, SIGNAL(clicked()), this, SLOT(onUploadClicked()));
}

void TUploadDialog::setupUploadingFrame()
{
    this->_uploadingFrame = new QFrame();
    QVBoxLayout* layout = new QVBoxLayout(_uploadingFrame);

    QLabel* label = new QLabel(_uploadingFrame);
    label->setText(tr("Document uploading"));
    label->setFixedHeight(25);
    layout->addWidget(label);

    _progress = new QProgressBar(_uploadingFrame);
    _progress->setRange(0, ProgressWeight);
    _progress->setFixedHeight(25);
    layout->addWidget(_progress);
}

void TUploadDialog::setupFinishedFrame()
{
    this->_finishedFrame = new QFrame();

    _uploadStateLbl = new QLabel(_finishedFrame);
    _uploadStateLbl->setFixedHeight(25);

    QPushButton* pbtReUpload = new QPushButton(_finishedFrame);
    pbtReUpload->setText(tr("Continue to upload"));
    pbtReUpload->setFixedHeight(25);

    QPushButton* pbtFinish = new QPushButton(_finishedFrame);
    pbtFinish->setText(tr("Finished"));
    pbtFinish->setFixedHeight(25);

    QSpacerItem* spacer =
            new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding);

    QHBoxLayout* hLayout = new QHBoxLayout();
    hLayout->addSpacerItem(spacer);
    hLayout->addWidget(pbtReUpload);
    hLayout->addWidget(pbtFinish);
    //hLayout->setStretch(1, 65535);

    QVBoxLayout* layout = new QVBoxLayout(_finishedFrame);
    layout->addWidget(_uploadStateLbl);
    layout->addLayout(hLayout);

    //signals
    connect(pbtReUpload, SIGNAL(clicked()), this, SLOT(onReUploadClicked()));
    connect(pbtFinish, SIGNAL(clicked()), this, SLOT(close()));
}

void TUploadDialog::onSelectFileClicked()
{
    _pbtSelect->setEnabled(false);
    _pbtUpload->setEnabled(false);
    QString filename = QFileDialog::getOpenFileName(this, tr("Load"), "",
                                                    tr("All files (*.*);;PPT (*.ppt *.pptx)"));
    _pbtSelect->setEnabled(true);
    _pbtUpload->setEnabled(true);

    if(filename.isEmpty())
    {
        return ;
    }

    _ledFile->setText(filename);

    _ledTitle->setText(QFileInfo(filename).fileName());
}

void TUploadDialog::onUploadClicked()
{
    if (this->_ledFile->text().trimmed().isEmpty())
    {
        TMessageBox dialog(tr("caution"),
                           tr("Please select a file first."),
                           QMessageBox::Ok,
                           QMessageBox::NoButton, this);
        dialog.centerWindow();
        dialog.exec();
        return ;
    }
    else if (this->_ledTitle->text().trimmed().isEmpty())
    {
        TMessageBox dialog(tr("caution"),
                           tr("Please input file title."),
                           QMessageBox::Ok,
                           QMessageBox::NoButton, this);
        dialog.centerWindow();
        dialog.exec();
        return ;
    }
    this->_selectFrame->hide();
    this->_uploadingFrame->show();
    this->update();

    _uploadRequester = TContext::instance()->requesterFactory()->createAdvUploadRequester();
    connect(_uploadRequester.data(), SIGNAL(uploadFinished(bool, TDocumentPointer)),
            this, SLOT(onUploadFinished(bool,TDocumentPointer)));
    connect(_uploadRequester.data(), SIGNAL(uploadProgress(qint64, qint64)),
            this, SLOT(onUploadProgress(qint64,qint64)));
    _uploadRequester->upload(this->_ledFile->text(),
                             this->_ledTitle->text(),
                             this->_cmbShare->currentIndex()==1 ? true : false);
}

void TUploadDialog::onUploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
    this->_progress->setValue((int)(bytesSent/(bytesTotal * 1.0))*ProgressWeight);
}

void TUploadDialog::onUploadFinished(bool error, TDocumentPointer pDoc)
{
    if(error)
    {
        _uploadStateLbl->setText(tr("Document upload failed"));
    }
    else
    {
        _uploadStateLbl->setText(tr("Document upload success"));
    }

    this->_uploadingFrame->hide();
    this->_uploadingFrame->update();
    this->_finishedFrame->show();
    this->update();

    emit uploadFinished();
}


void TUploadDialog::onReUploadClicked()
{
    this->_finishedFrame->hide();
    this->_selectFrame->show();
    this->update();
}
